package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import model.Users;

public class UsersDaoImpl implements UsersDao {
	PreparedStatement psmt=null;
	ResultSet rSet=null;
	@Override
	public Users findByLoginIdAndPwd(String loginId, String loginPwd) {
		String sql="select * from users where loginId=? and loginPwd=?";
		JdbcUtil.conn=JdbcUtil.getConn();
		try {
			psmt=JdbcUtil.conn.prepareStatement(sql);
			psmt.setString(1, loginId);
			psmt.setString(2, loginPwd);
			rSet=psmt.executeQuery();
			if (rSet.next()) {
				Users u=new Users();
				u.setId(rSet.getInt("id"));
				u.setLoginId(rSet.getString("loginId"));
				u.setLoginPwd(rSet.getString("loginPwd"));
				return u;
			}
		} catch (Exception e) {
			JdbcUtil.close(rSet, psmt, JdbcUtil.conn);
		}
		return null;
	}
	@Override
	public Users updateUsers(Users users) {
		String sql="update users set loginPwd ='"+users.getLoginPwd()+"' where id='"+users.getId()+"' ";
		JdbcUtil.conn=JdbcUtil.getConn();
		try {
			psmt=JdbcUtil.conn.prepareStatement(sql);

			psmt.execute(sql);
			if (rSet.next()) {
				Users u=new Users();
				u.setId(rSet.getInt("id"));
				u.setLoginId(rSet.getString("loginId"));
				u.setLoginPwd(rSet.getString("loginPwd"));
				return u;
			}
		} catch (Exception e) {
			JdbcUtil.close(rSet, psmt, JdbcUtil.conn);
		}
		return null;
	}

}
